import axios from 'axios';
import qs from 'qs';
import tokenStore from "@/stores/TokenStore.js";
import router from '/src/router/index'
//接口路径,如果访问当前的项目,决定使用 局域网IP,http://192.168.0.41:5173,搭配的服务地址,也必须是192.168.0.41:8080
let ip = "localhost:8080";
let websocketurl = "ws://"+ip+"/"
axios.defaults.baseURL = 'http://'+ip+"/"
//post请求头
axios.defaults.headers.post["Content-Type"] ="application/x-www-form-urlencoded;charset=UTF-8";
//axios发送跨域ajax请求时 携带cookie
axios.defaults.withCredentials = true
//设置超时
axios.defaults.timeout = 10000;

//请求
axios.interceptors.request.use(
    config => {
        let token = tokenStore().token;
        //console.log(token)
        if (token != null && token!==""){
            config.headers.javasm_token = token;
        }
        return config;
    },
    error => {
        return Promise.reject(error);
    }
);
//响应
axios.interceptors.response.use(
    response => {
        let token = response.headers.javasm_token;
        if (token != null && token!=""){
            tokenStore().token = token;
        }
        //服务器 给前端的内容
        if (response.status === 200) {
            return Promise.resolve(response);
        } else {
            return Promise.reject(response);
        }
    },
    error => {
        alert(`异常请求：${JSON.stringify(error.message)}`)
    }
);
//外面可以调用的方法
export default {
    //post方法
    post(url, data) {
        return new Promise((resolve, reject) => {
            axios({
                method: 'post',
                url,
                data: qs.stringify(data),//json数据 转换成  name=xiaoming&age=10&address=xxx
            }).then(res => {
                resolve(res.data)
            }).catch(err => {
                reject(err)
            });
        })
    },
    post2(url, data) {
        return new Promise((resolve, reject) => {
            axios({
                method: 'post',
                url,
                data: qs.stringify(data,{"indices":false}),
            }).then(res => {
                resolve(res.data)
            }).catch(err => {
                reject(err)
            });
        })
    },
    get(url, data) {
        return new Promise((resolve, reject) => {
            axios({
                method: 'get',
                url,
                params: data,
            }).then(res => {
                resolve(res.data)
            }).catch(err => {
                reject(err)
            })
        })
    },
    postJson(url,data){
        return new Promise((resolve, reject) => {
            axios({
                method: 'post',
                url,
                data: data,
                headers: {
                    'Content-Type': 'application/json',
                }
            }).then(res => {
                resolve(res.data)
            }).catch(err => {
                reject(err)
            })
        })
    },
    delete(url, data) {
        return new Promise((resolve, reject) => {
            axios({
                method: 'delete',
                url,
                params: data,
            }).then(res => {
                resolve(res.data)
            }).catch(err => {
                reject(err)
            })
        })
    },
    getSocketUrl(){
        return websocketurl;
    },
    getNow12(){
        const now = new Date();
        let hours = now.getHours();
        const minutes = now.getMinutes();
        const seconds = now.getSeconds();
        const ampm = hours >= 12 ? 'PM' : 'AM';

        // 将小时转换为12小时制
        hours = hours % 12;
        hours = hours ? hours : 12; // 如果小时为0，则设置为12

        // 格式化分钟和秒为两位数
        const formattedMinutes = minutes < 10 ? '0' + minutes : minutes;
        const formattedSeconds = seconds < 10 ? '0' + seconds : seconds;

        return `${hours}:${formattedMinutes}:${formattedSeconds} ${ampm}`;
    }
};